package com.example.hms.servicelmpl.servicelmpl;

import com.example.hms.dao.DashboardDao;
import com.example.hms.entity.Dashboard;
import com.example.hms.servicelmpl.DashboardService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class DashboardServicelmpl implements DashboardService {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
    @Resource
    private DashboardDao dao;
    @Override
    public  Result selectRoomNumber(){
        return ResultSubmit.success(dao.selectRoomNumber());
    }
    @Override
    public Result selectLiveNumber(){
        return ResultSubmit.success(dao.selectLiveNumber());
    }

    public Result selectMoney(String month){
        month = month + "-01 00:00:00";
        return ResultSubmit.success(dao.selectMoney(month));
    }
    @Override
    public Result selectMoneyByMonth(String month){
        List<Dashboard> 多月金额数据= new ArrayList<>();
        for(int i=0;i<6;i++){
            Dashboard 单月金额 = new Dashboard();
            单月金额.setMonth(month);
            单月金额.setMoney(dao.selectMoney(month +"-01 00:00:00"));
            多月金额数据.add(单月金额);
            Date date =null;
            try{
                date=format.parse(month+"-01 00:00:00");

            }catch (ParseException e){
                e.printStackTrace();
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH,-1);
            month =format.format(calendar.getTime());

        }
        return ResultSubmit.success(多月金额数据);
    }
}
